import { userAccountLogin } from '@/api/user'
import message from '@/components/Message/index'
import router from '@/router'
export default {
  namespaced: true,
  state: () => ({
    profile: {
      id: '',
      nickname: '',
      avatar: '',
      token: '',
      mobile: ''
    }
  }),
  mutations: {
    setProfile (state, payload) {
      // 将来登录成功之后 把用户数据通过调用mutation函数的形式添加过来
      console.log(payload)
      const { id, nickname, avatar, token, mobile } = payload.data.result
      state.profile.id = id
      state.profile.nickname = nickname
      state.profile.token = token
      state.profile.avatar = avatar
      state.profile.mobile = mobile
    },
    clearProfile (state) {
      state.profile = {
        id: '',
        nickname: '',
        avatar: '',
        token: '',
        mobile: ''
      }
    }
  },
  actions: {
    async asyncSetProfile (ctx, { account, password }) {
      // 1. 调用登录接口
      const res = await userAccountLogin({ account, password })
      // 2. 数据成功之后通过调用mutation函数存入state
      ctx.commit('setProfile', res)
      router.push('/')
      // 提示用户
      message({ type: 'success', text: '登陆成功' })
    }
  },
  getters: {}
}
